﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 坐标转换工具
/// </summary>
public class LocationTran{

    /// <summary>
    /// 经纬度转换屏幕坐标
    /// </summary>
    /// <param name="lat"></param>
    /// <param name="lon"></param>
    /// <returns>经纬度</returns>
    public static int[] LatLon2XY(double lat,double lon) {
        if (MapController.mapConfigure == null)
        {
            Debug.Log("地图未配置最大最小宽度，无法转换坐标");

            return null;
        }
        double scaleX = ((MapController.mapConfigure.MapRightBorder.Longitude - MapController.mapConfigure.MapLeftBorder.Longitude) * 3600) 
            / MapController.mapConfigure.Height;
        double scaleY = ((MapController.mapConfigure.MapRightBorder.Latitude - MapController.mapConfigure.MapLeftBorder.Latitude) * 3600)
            / MapController.mapConfigure.Width;

        double minX = MapController.mapConfigure.MapLeftBorder.Longitude * 3600 / scaleX;
        double minY = MapController.mapConfigure.MapLeftBorder.Latitude * 3600 / scaleY;

        double X = (lon - MapController.mapConfigure.MapLeftBorder.Longitude) * 3600 / scaleX;
        double Y = (MapController.mapConfigure.MapRightBorder.Latitude - lat) * 3600 / scaleY;

        return new int[2] { (int)X, (int)Y };
    }
    /// <summary>
    /// 经纬度转换屏幕坐标
    /// </summary>
    /// <param name="lat"></param>
    /// <param name="lon"></param>
    /// <returns>二维屏幕坐标</returns>
    public static double[] XY2LatLon(int x, int y)
    {
        if (MapController.mapConfigure == null)
        {
            Debug.Log("地图未配置最大最小宽度，无法转换坐标");

            return null;
        }
        double scaleX = ((MapController.mapConfigure.MapRightBorder.Longitude - MapController.mapConfigure.MapLeftBorder.Longitude) * 3600)
            / MapController.mapConfigure.Height;
        double scaleY = ((MapController.mapConfigure.MapRightBorder.Latitude - MapController.mapConfigure.MapLeftBorder.Latitude) * 3600)
            / MapController.mapConfigure.Width;

        double minX = MapController.mapConfigure.MapLeftBorder.Longitude * 3600 / scaleX;
        double minY = MapController.mapConfigure.MapLeftBorder.Latitude * 3600 / scaleY;

        double lon = x * scaleX / 3600 + MapController.mapConfigure.MapLeftBorder.Longitude;
        double lat = MapController.mapConfigure.MapRightBorder.Latitude - y * scaleY / 3600;

        return new double[2] { lat ,lon};
    }
}
